<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * validation rules for user
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->route('user');
        if($this->isMethod('post')){ // when create
            return [
                'name' => 'required|between:5,18|unique:users',
                'email' => 'required|email|max:255|unique:users',
                'password' => 'required|between:6,18|confirmed',
            ];
        }else{ //when update
            return [
                'name' => 'required|between:5,18|unique:users,name,'.$id,
                'email' => 'email|max:255|unique:users,email,'.$id,
                'password'=>'between:6,18'
            ];
        }

    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => _("请输入用户名"),
            'name.between'  => _("用户名长度在:min 与 :max.之间"),
            'name.unique'  => _("用户名已存在"),
            'password.between'  => _("密码长度在:min 与 :max.之间"),
            'password.required'  => _("请输入密码"),
            'password.confirmed'  => _("两次密码不一致"),
            'email.required'  => _("请输入邮箱地址"),
            'email.email'  => _("邮箱地址不合法"),
            'email.max'  => _("邮箱地址长度不能超过255"),
            'email.unique'  => _("此邮箱地址已存在"),
        ];
    }
}
